package com.wennimei.service;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.xml.sax.InputSource;

import com.wennimei.domain.Music;
import com.wennimei.util.HttpUtil;

@Service
public class MusicService {

    @SuppressWarnings("unchecked")
    public Music get(String content) {
        try {
            String[] array = content.split("唱的");
            String url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=%s$$%s$$$$";
            url = String.format(url, URLEncoder.encode(array[1], "GBK"),
                    URLEncoder.encode(array[0], "GBK"));
            InputStream in = HttpUtil.getStream(url);
            BufferedReader br = new BufferedReader(new InputStreamReader(in,
                    "GBK"));
            String xmlString = "";
            for (String temp = br.readLine(); temp != null; xmlString += temp, temp = br
                    .readLine());
            String encode = null;
            String decode = null;
            String p2p = null;
            InputStream stream2 = new ByteArrayInputStream(
                    xmlString.getBytes("GBK"));
            SAXReader saxReader = new SAXReader();
            saxReader.setEncoding("GBK");
            Document doc = (Document) saxReader.read(new InputSource(stream2));
            Element e = doc.getRootElement();
            for (Iterator<Element> i = e.elementIterator(); i.hasNext();) {
                Element element = (Element) i.next();
                if (element.getName().equals("url")) {
                    for (Iterator<Element> j = element.elementIterator(); j
                            .hasNext();) {
                        Element u = (Element) j.next();
                        if (u.getName().equals("encode"))
                            encode = (String) u.getData();
                        else if (u.getName().equals("decode"))
                            decode = (String) u.getData();
                    }
                } else if (element.getName().equals("p2p")) {
                    for (Iterator<Element> j = element.elementIterator(); j
                            .hasNext();) {
                        Element u = (Element) j.next();
                        if (u.getName().equals("url"))
                            p2p = (String) u.getData();
                    }
                }
            }

            String lowUrl = "";
            String highUrl = "";
            if (null == encode || null == decode || encode.isEmpty()
                    || decode.isEmpty())
                lowUrl = null;
            if (null == p2p || p2p.trim().isEmpty())
                highUrl = null;

            if (null == lowUrl && null == highUrl)
                return null;

            if (null != lowUrl)
                lowUrl = this.getLowUrl(encode, decode);

            if (null == highUrl)
                highUrl = lowUrl;
            Music music = new Music(array[0], array[1], lowUrl, highUrl);
            return music;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getLowUrl(String encode, String decode) {
        Pattern pattern = Pattern.compile("^(\\d+).*");
        Matcher matcher = pattern.matcher(decode);
        String name = null;
        while (matcher.find()) {
            name = matcher.group(1);
            break;
        }
        encode = encode.replaceAll(name + "/.*", "");

        return encode + decode;
    }

}
